/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.roadrunner;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.roadrunner.listOrderFragment.OnHeadlineSelectedListener;

import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.Spinner;
import android.widget.TextView;

public class OrderFragment extends Fragment implements FragmentCommunicator{
    final static String ARG_POSITION = "position";
    int mCurrentPosition = -1;

    private String stateSaveURL = "";
    private ActivityCommunicator activityCommunicator;
    public Context context;
    
    private TextView o = null;
    private TextView c = null;
    private TextView p = null;
    private TextView d = null;
    private TextView s = null;
    private String userName = "";
    
    /* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onAttach(android.app.Activity)
	 */
    @Override
    public void onAttach(Activity activity)
    {
    	super.onAttach(activity);
		 
    	context = getActivity();
    	activityCommunicator = (ActivityCommunicator)context;
    	((JobsList)context).fragmentCommunicator = this;
    }

	private Spinner spinner = null; 
    private ArrayList<Job> jobs = null;	
    private Job selectedOrder = null;
    		
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {

        // If activity recreated (such as from screen rotate), restore
        // the previous article selection set by onSaveInstanceState().
        // This is primarily necessary when in the two-pane layout.
        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
        }
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.order_fragment, container, false);
    }

    /* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onActivityCreated(android.os.Bundle)
	 */
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
		 o = (TextView) getActivity().findViewById(R.id.orderID);
		 c = (TextView) getActivity().findViewById(R.id.Custommer);
		 p = (TextView) getActivity().findViewById(R.id.PickUp);
		 d = (TextView) getActivity().findViewById(R.id.Destination);
		 s = (TextView) getActivity().findViewById(R.id.status);
		 userName = JobsList.curentUserName;
		 
		try {

			stateSaveURL = ""; 
			
			Button SaveButton = (Button) getActivity().findViewById(R.id.stateSave);
	        if(SaveButton != null) {
	        	OnClickListener buttonClickListener = new OnClickListener(){
		            @Override
		            public void onClick(View arg0) {
		                //Do something
		            	
		            	String newState = "";
		            	
		            	if(selectedOrder.getJobState().compareTo(JobState.NOT_ASSIGNED.toString()) == 0)
		            	{
		            		newState = JobState.RESERVED.toString();
		            		selectedOrder.setJobState(JobState.RESERVED.toString());
		            		s.setText(selectedOrder.getJobState());
		            	}
		            	else if(selectedOrder.getJobState().compareTo(JobState.RESERVED.toString()) == 0)
			            {
		            		newState = JobState.PICKED_UP.toString();
		            		selectedOrder.setJobState(JobState.PICKED_UP.toString());
		            		s.setText(selectedOrder.getJobState());
			            }
		            	else if(selectedOrder.getJobState().compareTo(JobState.PICKED_UP.toString()) == 0)
			            {
		            		newState = JobState.DELIVERED.toString();
		            		selectedOrder.setJobState(JobState.DELIVERED.toString());
		            		s.setText(selectedOrder.getJobState());
			            }
			            
		            	
		            	
		            	stateSaveURL = "editjobstate,orderId_editjobstate," + selectedOrder.getOrderId() + ",jobState_edit," + newState;
		            	
		            			            	
		            	if(stateSaveURL.length() > 0)
		            		activityCommunicator.passDataToActivity(stateSaveURL);
		            	stateSaveURL = "";
		            	
		            	updateArticleView(mCurrentPosition);
		            	
		            	return;
		            }           
		        };
		        
		        SaveButton.setOnClickListener(buttonClickListener);
	        }
	        
	    } catch (ClassCastException e) {
	        e.printStackTrace();
	    }
		
	}

	@Override
    public void onStart()
    {
      	super.onStart();
        
        Bundle args = getArguments();
        if (args != null) 
        {
            // Set article based on argument passed in
            updateArticleView(args.getInt(ARG_POSITION));
        } 
        else if (mCurrentPosition != -1) 
        {
            // Set article based on saved instance state defined during onCreateView
            updateArticleView(mCurrentPosition);
        }
    }

    public void updateArticleView(int position) {    	       
        
        jobs = JobsList.getJobList();
        String fromActiv = getArguments().getString("FROM");
        
        selectedOrder = jobs.get(position);
        
        Long id = new Long(selectedOrder.getOrderId());
        o.setText(id.toString());
        c.setText(selectedOrder.getExpName());
        p.setText(selectedOrder.getExpAdress());
        d.setText(selectedOrder.getDestAdress());
        s.setText(selectedOrder.getJobState());
        
        
        Button SaveButton = (Button) getActivity().findViewById(R.id.stateSave);
        
        if( (selectedOrder.getJobState().compareTo(JobState.NOT_ASSIGNED.toString()) != 0)
        	&& (selectedOrder.getTransporterName().compareTo(userName) != 0))
        {
        	SaveButton.setVisibility(View.GONE); 
        }
        else
        {        
	        if(selectedOrder.getJobState().compareTo(JobState.NOT_ASSIGNED.toString()) == 0)
	    	{
	        	SaveButton.setText("RESERVE");
	    		s.setText(selectedOrder.getJobState());
	    	}
	    	else if(selectedOrder.getJobState().compareTo(JobState.RESERVED.toString()) == 0)
	        {
	    		SaveButton.setText("PICK UP");
	    		s.setText(selectedOrder.getJobState());
	        }
	    	else if(selectedOrder.getJobState().compareTo(JobState.PICKED_UP.toString()) == 0)
	        {
	    		SaveButton.setText("DELIVER");
	    		s.setText(selectedOrder.getJobState());
	        }
	    	else
	    	{
	    		SaveButton.setVisibility(View.GONE); 
	    	}
        }

        
        mCurrentPosition = position;
    }


   
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // Save the current article selection in case we need to recreate the fragment
        outState.putInt(ARG_POSITION, mCurrentPosition);
    }

	@Override
	public void passDataToFragment(String someValue) {
		// TODO Auto-generated method stub
		
	}
}